<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


class ClcwCarouselLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $clcwCarouselModel;

    public function _initialize()
    {
        $this->clcwCarouselModel = D('ClcwCarousel', 'Model');
    }

    /**
     * 获得焦点图列表
     *
     * @param int $page
     * @param int $size
     * @param     $params
     *
     * @return array
     */
    public function getCarouselList($page = 1, $size = 10, $params)
    {
        $map = [];
        if ($params['id']) {
            $map['id'] = ['LIKE', "%" . $params['id'] . "%"];
        }
        if ($params['title']) {
            $map['title'] = ['LIKE', "%" . $params['type'] . "%"];
        }
        if ($params['type']) {
            $map['type'] = ['eq', $params['type']];
        }
        if ($params['start_time']) {
            $map['create_time'] = ['egt', $params['start_time']];
        }
        if ($params['end_time']) {
            $map['create_time'] = ['elt', $params['end_time']];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['create_time'] = ['BETWEEN', [$params['start_time'], $params['end_time']]];
        }
        if ($params['sort']) {
            $map['sort'] = ['LIKE', "%" . $params['sort'] . "%"];
        }
        if ($params['title']) {
            $map['title'] = ['LIKE', "%" . $params['title'] . "%"];
        }
        if ($params['status']) {
            $map['status'] = ['eq', $params['status']];
        }
        if ($params['platform']) {
            $map['platform'] = ['eq', $params['platform']];
        }

        $cols = 'id,title,focus_picture,type,param_url,scene_id,sort,status,platform,create_time';
        $order = 'status,sort,id DESC';
        $total = $this->clcwCarouselModel->where($map)->count();
        $list = $this->clcwCarouselModel->field($cols)
            ->where($map)
            ->limit($size)
            ->page($page)
            ->order($order)
            ->select();
//        echo $this->clcwCarouselModel->getLastSql();
        $listInfo = [];
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $listInfo[$k]['id'] = $v['id'];
                $listInfo[$k]['type'] = $v['type'];
                $listInfo[$k]['type_name'] = C('CLCW_CAROUSEL_TYPE')[$v['type']];
                $listInfo[$k]['title'] = $v['title'];
                $listInfo[$k]['focus_picture'] = $v['focus_picture'];
                $listInfo[$k]['param_url'] = $v['param_url'];
                $listInfo[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
                $listInfo[$k]['sort'] = $v['sort'];
                $listInfo[$k]['status'] = $v['status'];
                $listInfo[$k]['status_name'] = C('CLCW_CAROUSEL_STATUS')[$v['status']];
                $listInfo[$k]['platform'] = $v['platform'];
            }
        }
        $result = [
            'list'  => $listInfo,
            'total' => $total
        ];
        return $result;
    }


    /**
     * 根据平台获取轮播图
     *
     * @param int $platform
     *
     * @return mixed
     */
    public function getCarouselByPlatform($platform = 0)
    {
        if (empty($platform)) {
            return false;
        }
        $condition['platform'] = $platform;
        $condition['status'] = 1;
        return $this->clcwCarouselModel->where($condition)->select();
    }

    /**
     * 根据轮播图ID获取详情
     *
     * @param int $id
     *
     * @return mixed
     */
    public function getCarouselById($id = 0)
    {
        if (empty($id)) {
            return false;
        }
        $condition = [];
        $condition['id'] = $id;
        return $this->clcwCarouselModel->where($condition)->find();
    }


    /**
     * 添加新建轮播图
     *
     * @param array $data
     *
     * @return bool
     */
    public function addCarousel($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $data['create_time'] = NOW_TIME;
        $data['update_time'] = NOW_TIME;
        return $this->clcwCarouselModel->add($data);
    }

    /**
     * 编辑更新辑轮播图
     *
     * @param array $data
     *
     * @return bool
     */
    public function saveCarousel($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $data['update_time'] = NOW_TIME;
        return $this->clcwCarouselModel->save($data);
    }


    /**
     * 上下线更新状态操作
     *
     * @param int $id
     * @param int $sort
     * @param int $status
     * @param int $platform
     *
     * @return bool
     */
    public function updateCarouselStatus($id = 0, $sort = 0, $status = 0, $platform = 0)
    {
        if (empty($id)) {
            return false;
        }
        $condition = [];
        $condition['id'] = $id;
        $data['status'] = $status;
        if ($status == '1') {     //如果上线操作
            $map['status'] = '1';
            $map['sort'] = $sort;
            $map['platform'] = $platform;
            $total = $this->clcwCarouselModel->where($map)->count();
            if ($total > 0) {
                return false;
            }
        }
        return $this->clcwCarouselModel->where($condition)->setField($data);
    }

    /**
     * 判断排序是否在上线状态中的焦点图是否存在
     *
     * @param int $sort
     * @param int $platform
     *
     * @return bool
     */
    public function isSortOnline($sort = 0, $platform = 0)
    {
        if (empty($sort) || empty($platform)) {
            return false;
        }
        $condition['status'] = '1';
        $condition['sort'] = $sort;
        $condition['platform'] = $platform;
        return $this->clcwCarouselModel->where($condition)->count();
    }
}